<%--
PC浏览器展示二维码，等待微信端通过webSocket返回信息，进行功能导航
--%>
<%@ page import="java.util.UUID" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="head.jsp" %>
    <script src="/uitl.js"></script>
</head>
<body>

<%
    UUID uuid = UUID.randomUUID();
    String strUUID = uuid.toString().replace("-", "").substring(1, 5);
%>

<input type="hidden" value="<%=strUUID%>" id="uuid">
<div class="weui-media-box__hd">
    <img width="150px" class="weui-media-box__thumb"
         src="/qrcode/wxredirect?uuid=<%=strUUID%>">
</div>
<script>
    $(document).ready(function () {
        connect();
    });

    var ws = null;
    var clientID = $("#uuid").val();

    function connect() {
        if (clientID != null) {
            if ('WebSocket' in window) {
                ws = new WebSocket("ws://auth.jkgly.cn:8089/socketServer/" + clientID);
            } else if ('MozWebSocket' in window) {
                ws = new MozWebSocket("ws://auth.jkgly.cn:8089/socketServer/" + clientID);
            } else {
                alert("该浏览器不支持websocket");
            }

            ws.onmessage = function (evt) {
                console.log(evt.data);
                var loginInfo = evt.data.toString().split("|");
                if (loginInfo.length == 0) {
                    alert(evt.data);
                } else {
                    var login_url = loginInfo[1];//"http://bi.jkgly.cn:80/WebReport/ReportServer?op=fr_bi&cmd=bi_init&id=48&createBy=103"; //
                    var target_url = loginInfo[2];
                    var user = loginInfo[3];
                    var psd = loginInfo[4];
                    commLogin2(user, psd, login_url, target_url);
                }
            };

            ws.onclose = function (evt) {
            };

            ws.onopen = function (evt) {
            };

        } else {
            alert("clientID异常");
        }
    }

    function commLogin2(username, password, login_url, urlTarget) {
        jQuery.ajax({
            url: login_url,//"http://bi.jkgly.cn/WebReport/ReportServer?op=fs_load&cmd=sso",//单点登录的报表服务器
            dataType: "jsonp",//跨域采用jsonp方式
            data: {"fr_username": username, "fr_password": password},
            jsonp: "callback",
            timeout: 5000,//超时时间（单位：毫秒）
            success: function (data) {
                if (data.status === "success") {
                    console.log("success"); //登录成功
                    data.url = urlTarget //赋值跳转的url，上面var生成，方便管理
                    window.location = data.url;//登录成功，直接跳转到平台系统页面
                } else if (data.status === "fail") {
                    alert("用户名密码错误！！！");           //登录失败（用户名或密码错误）
                }
            },
            error: function (msg) {
                console.log(msg);
                // $.alert(msg);// 登录失败（超时或服务器其他错误）
            }
        });
    }
</script>
</body>
</html>
